package com.changgou.common.pojo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.Serializers;
import lombok.Data;
import org.apache.catalina.User;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Data
public class BaseResult<T> {

    public static void main(String[] args) {
        System.out.println(new BaseResult());
        System.out.println(new BaseResult("-20001"));
        Users users =new Users();
        users.setUserId("1");
        users.setUserName("30");
        System.out.println(new BaseResult(users));
    }

    public BaseResult getUserById(String userId){
        String status=null;
        if(null == userId  || "".equals(userId)){
            status = "-20001";
        }
        Users users=null;
        if(status == null){
            //查询操作
            users =new Users();
            users.setUserId("1");
            users.setUserName("30");
        }
        return new BaseResult(status,users);
    }
    private static ConcurrentMap<String,String> responseData=new ConcurrentHashMap<>();

    private String code;
    private String msg;
    private Date responseTime;
    private T data;

    public BaseResult(){
        this.code="200";
        this.setMsg(getResponseMSG(this.code));
        responseTime =new Date();
    }

    public BaseResult(T data){
        this.code = "200";
        this.setMsg(getResponseMSG(this.code));
        responseTime =new Date();
        this.data = data;
    }
    public BaseResult(String code){
        this.code=code;
        this.setMsg(getResponseMSG(this.code));
        responseTime =new Date();
    }

    public BaseResult(String code,T date){
        this.code = code==null?"200":code;
        this.setMsg(getResponseMSG(this.code));
        responseTime =new Date();
        this.data = date;
    }

    static {
        initResponseData();
    }

    private String getResponseMSG(String code){
        return this.responseData.get(code);
    }

    private static void initResponseData(){
        responseData.put("200","操作成功!");
        responseData.put("-10000","操作失败!");
        responseData.put("-20001","用户名不能为空!");
        responseData.put("-20002","电话号码不能为空!");
        responseData.put("-20003","支付金额不能为空!");
        responseData.put("-20004","短信验证码失效/错误!");

    }
}
